<template>
    <div id="detail">
      <detail-navbar @titleClick="titleClick" ref="nav"></detail-navbar>
      <Scroll class="wrapper" ref="scroll" @scroll="contentScroll">
        <detail-swiper :top-images="topImages"></detail-swiper>
        <detail-base-info :goods="goods"></detail-base-info>
        <detail-shop-info :shop="shop"></detail-shop-info>
        <detail-goods-info :detail-info="detailInfo" @imageLoad="imageLoad"></detail-goods-info>
        <detail-param-info :param-info="paramInfo" ref="params"></detail-param-info>
        <detail-comment-info :comment-info="commentInfo" ref="comment"></detail-comment-info>
        <goods-list :goods = recommends ref="recommends"></goods-list>
      </Scroll>
      <detail-bottom-bar @addCart="addToCart"></detail-bottom-bar>
      <back-top v-show="isShowBackTop" @click.native="backClick"></back-top>
    </div>
</template>

<script>
import DetailNavbar from './childComps/DetailNavbar'
import DetailSwiper from './childComps/DetailSwiper'
import DetailBaseInfo from './childComps/DetailBaseInfo'
import DetailShopInfo from './childComps/DetailShopInfo'
import Scroll from 'components/common/scroll/Scroll'
import DetailGoodsInfo from './childComps/DetailGoodsInfo'
import DetailParamInfo from './childComps/DetailParamInfo'
import DetailCommentInfo from './childComps/DetailCommentInfo'
import GoodsList from 'components/content/goods/GoodsList'
import DetailBottomBar from './childComps/DetailBottomBar'

import { getDetail, Goods, Shop, GoodsParam, getRecommend } from 'network/detail'

import { itemListenerMixin, backTopMixin } from 'common/mixin'
import { debounce } from '../../common/util'
import { mapActions } from 'vuex'

export default {
  name: 'Detail',
  components: {
    DetailNavbar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    Scroll,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    GoodsList,
    DetailBottomBar
  },
  mixins: [itemListenerMixin, backTopMixin],
  data () {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommends: [],
      themeTopYs: [],
      getThemeTopY: null,
      currentIndex: 0
    }
  },
  created () {
    // 1.保存传入的id
    this.iid = this.$route.params.iid
    // 2.根据id请求详情数据
    getDetail(this.iid).then(res => {
      // console.log(res)
      const data = res.result
      // 1.获取顶图的轮播图
      this.topImages = data.itemInfo.topImages
      // 2.获取商品信息
      this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services)
      // 3.获取店铺信息
      this.shop = new Shop(data.shopInfo)
      // 4. 获取商品的详细信息
      this.detailInfo = data.detailInfo
      // 5. 获取商品参数信息
      this.paramInfo = new GoodsParam(data.itemParams.info, data.itemParams.rule)
      // 6. 获取评论信息
      if (data.rate.cRate !== 0) {
        this.commentInfo = data.rate.list[0]
      }
      // 3.请求推荐数据
      getRecommend().then(res => {
      // console.log(res.data)
        this.recommends = res.data.list
      })
      // 4. 为topYs设置防抖
      this.getThemeTopY = debounce(() => {
        this.themeTopYs = []
        this.themeTopYs.push(0)
        this.themeTopYs.push(this.$refs.params.$el.offsetTop)
        this.themeTopYs.push(this.$refs.comment.$el.offsetTop)
        this.themeTopYs.push(this.$refs.recommends.$el.offsetTop)
        this.themeTopYs.push(Number.MAX_VALUE) // 在数组末尾增加一个无限大的值，为了之后对数组做遍历
        // console.log(this.themeTopYs)
      }, 500)
    })
  },
  destroyed () {
    // 离开组件时取消图片加载监听事件
    this.$bus.$off('itemImageLoad', this.itemImgListener)
  },
  methods: {
    ...mapActions([
      'addCart'
    ]),
    imageLoad () {
      this.$refs.scroll.refresh()
      this.getThemeTopY()
    },
    titleClick (index) {
      // console.log(index)
      this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 500)
    },
    contentScroll (position) {
      // 1.获取y值
      const positionY = -position.y
      // 2.将y值与组件内容高度做对比
      // console.log(this.topYs[0]); // 这里是undefined，得不到它的值，并且即使得到了也应该为负值，
      // console.log(positionY)
      const length = this.themeTopYs.length
      // for(let i in this.topYs)  此方法获取的 i为字符串 要用parseInt(i) 或者 i*1 的方法将其转为数值
      // for(let i=0;i<length;i++){
      // if(this.topYs[i].positionY >=0 && this.topYs[i].positionY < this.topYs[i+1].positionY)
      //   console.log(this.topYs[i]);
      // console.log(this.topYs.length);
      // 1.普通做法
      // if(this.currentIndex !== i && ((i < length -1 && positionY >= this.topYs[i] && positionY < this.topYs[i+1]) || (i === length -1 && positionY >= this.topYs[i]))){
      // 2.hack做法，在数组后面加一个无限大的值，并且遍历时只遍历到 i<length-1
      for (let i = 0; i < length - 1; i++) {
        if (this.currentIndex !== i && (i < length - 1 && positionY >= this.themeTopYs[i] && positionY < this.themeTopYs[i + 1])) {
          this.currentIndex = i
          // console.log(i) // 因为此时打印非常频繁，所以将当前index保存为一个变量，在两个不等的情况下才打印，就不会很频繁
          // console.log(this.themeTopYs[i]) // 此处的值是正值
          this.$refs.nav.currentIndex = this.currentIndex
        }
      }
      // 判断是否显示backTop按钮
      this.isShowBackTop = (positionY) > 1000
    },
    addToCart () {
      // 1.获取购物车需要展示的信息，因为有多个信息，所以可以放在一个对象里
      const product = {}
      product.image = this.topImages[0]
      product.title = this.goods.title
      product.desc = this.goods.desc
      product.price = this.goods.realPrice
      product.iid = this.iid // id一定要传，因为id是商品的唯一标识，是将id传给服务器获取到对应的商品

      // this.$store.dispatch('addCart', product).then(res => {
      //   console.log(res)
      // })
      this.addCart(product).then(res => {
        this.$toast.show(res, 1500)
      })
    }
  }
}
</script>

<style scoped lang="scss">
  #detail{
    position: relative;
    z-index: 99;
    background-color: #fff;
    height: 100vh;
    .wrapper{
      /*position: absolute;*/
      /*top: 44px;*/
      /*left: 0;*/
      /*right: 0;*/
      /*bottom: 44px;*/
      /*overflow: hidden;*/
      height: calc(100% - 44px - 44px);
      overflow: hidden;
    }
  }
</style>
